﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Clipping.Infrastructure.Models;
using Clipping.Utils;

namespace ClippingEditor
{
    /// <summary>
    /// Interaction logic for CategorySelector.xaml
    /// </summary>
    public partial class CategorySelector : UserControl
    {
        public event Action<ClientInfo> SelectedClientChanged;

        public CategorySelector()
        {
            InitializeComponent();
        }

        List<Clipping.Infrastructure.Models.ClassInfo> selectedCategories;
        public List<Clipping.Infrastructure.Models.ClassInfo> SelectedCategories
        {
            get
            {
                return selectedCategories;
            }
            set
            {
                selectedCategories = value;
                udpateDisplay();
            }
        }

        private void udpateDisplay()
        {
            StringBuilder sb = new StringBuilder();
            if (selectedCategories != null)
            {
                foreach (var c in selectedCategories)
                {
                    sb.AppendLine(c.FullPath);
                }
            }
            this.tbx_SelectedCategories.Text = sb.ToString();
        }

        private void btn_Select_Click(object sender, RoutedEventArgs e)
        {
            var clientInfoProvider = Clipping.Utils.ClientInfoProvider.GetInstance();
            var userInfoProvider = Clipping.Utils.UserInfoProvider.GetInstance();
            List<Clipping.Infrastructure.Models.ClientInfo> allClientInfos = null;
            try
            {
                allClientInfos = clientInfoProvider.GetAllClientInfos();
            }
            catch (Exception ex)
            {
               // GlobalContext.LogHelper.LogErrMsg(ex, "读取客户信息失败");
                MessageBox.Show("读取客户信息失败，请重新下载客户信息后重新尝试", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (allClientInfos != null)
            {

                SettingRepository rep = new SettingRepository();
                var sysSettings = rep.GetSysSettings();
                var userSetting = rep.GetUserSettings();
                if (sysSettings != null && !string.IsNullOrEmpty(userSetting.UserName))
                {

                    try
                    {
                        var dic = userInfoProvider.GetAllUserInfoDic();
                        UserInfo ui;
                        if (dic.TryGetValue(userSetting.UserName, out ui))
                        {
                            allClientInfos = allClientInfos.Where(ci => ui.clientIds.Contains(ci.Id)).ToList();
                        }
                    }
                    catch (Exception ex)
                    {


                    }

                }
                Dlg_SelectCategories dlg = new Dlg_SelectCategories(allClientInfos, selectedCategories);
                if (dlg.ShowDialog() == true)
                {
                    ClientInfo oriCi = null;
                    if (this.SelectedCategories != null && this.SelectedCategories.Count > 0)
                        oriCi = this.SelectedCategories.First().Client;
                    ClientInfo newCi = null;
                    if (dlg.SelectedCategories != null && dlg.SelectedCategories.Count > 0)
                        newCi = dlg.SelectedCategories.First().Client;

                    this.SelectedCategories = dlg.SelectedCategories;
                    if (newCi != oriCi)
                        OnSelectedClientChagned(newCi);
                }
            }
            else
            {
                if (!clientInfoProvider.IsClientInfoDownloaded())
                {
                    if (MessageBox.Show("尚未下载客户信息，是否打开客户信息下载界面？", "", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
                    {
                        Settings.MainWindow win = new Settings.MainWindow(2);
                        win.ShowDialog();
                    }
                }
            }
        }

        private void OnSelectedClientChagned(ClientInfo client)
        {
            var handler = this.SelectedClientChanged;
            if (handler != null)
            {
                handler(client);
            }
        }

    }
}
